//
//  LMTabbar.m
//  彩票
//
//  Created by apple on 15/9/16.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMTabbar.h"

@interface LMTabbar()
@property (nonatomic,weak) UIButton *selectedBtn;

@end

@implementation LMTabbar



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (void)addTabbarBtnWithNornalImage:(NSString *)normalImg selImg:(NSString *)hightimage
{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setBackgroundImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightimage] forState:UIControlStateSelected];
    
    
    btn.tag = self.subviews.count;
    [btn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    if (btn.tag ==0) {
        
        [self selectedBtn:btn];
    }
    
    [self addSubview:btn];
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger count = self.subviews.count;
    CGFloat btnW = self.bounds.size.width/count;
    CGFloat btnH = self.bounds.size.height;
    for (UIButton *btn in self.subviews) {
        btn.frame = CGRectMake(btnW*btn.tag, 0, btnW, btnH);
    }

}


- (void)selectedBtn:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(tabbar:didSelectedFrom:to:)]) {
        [self.delegate tabbar:self didSelectedFrom:self.selectedBtn.tag to:btn.tag];
    }
    
    
    NSLog(@"%ld",btn.tag);
    self.selectedBtn.selected = NO;
    
    btn.selected = YES;
    self.selectedBtn = btn;
    

}
@end
